﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F4Plan : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        public panel_F4Plan()
        {
            InitializeComponent();
            
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);

            LoadData();

            this.F4Plan.DoubleClick += F5Plan_DoubleClick;
            this.F4Plan.PopupMenuShowing += F4Plan_PopupMenuShowing;
            this.f4planContext.ItemClicked += f5planContext_ItemClicked;
            
        }

        ///////////////////////////////////
        /// Call Edit Form
        ///////////////////////////////////
        private void callEditForm()
        {
            DataRow row = F4Plan.GetFocusedDataRow();
            /*
            if (!string.IsNullOrEmpty(row[7].ToString()))
            {
                MessageBox.Show("This plan has been Finished!");
            }
            else
            {
             */
            frm_Edit_F4Plan edit_Form = new frm_Edit_F4Plan();
            edit_Form.planid = row[0].ToString();
            if (edit_Form.LoadData())
            {
                edit_Form.ShowDialog();
                LoadData();
            }
            else
            {
                MessageBox.Show("Cannot load data for selected record");
            }
            //}
        }

        void F5Plan_DoubleClick(object sender, EventArgs e)
        {
            callEditForm(); 
        }

        void f5planContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f4planContext.Hide();
            DataRow row = F4Plan.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {                
                case "editSelection":
                    callEditForm();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }
 

        void F4Plan_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f4planContext.Show(MousePosition);
        }

        ///////////////////////////////////
        /// Load Data From Local Database
        ///////////////////////////////////
        private void LoadData()
        {
            ModuleWaitDialog.CreateWaitDialog("", "Please waiting...");
            ModuleWaitDialog.SetWaitDialogCaption("Loading data...");
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT [231_TimePlan].TimePlanNo, [231_TimePlan].PlanNo, [231_TimePlan].CompID, [231_TimePlan].NewComp,"
                + " [231_TimePlan].Quantity, [231_TimePlan].SubOrder1, [231_TimePlan].SubOrder2, [231_TimePlan].SubOrder3,"
                + " [231_TimePlan].SubOrder4, [231_TimePlan].SubOrder5, [231_TimePlan].SubOrder6,[222_Customer].FullName,"
                + " [231_TimePlan].FromSerialNo, [231_TimePlan].ToSerialNo, [231_TimePlan].DeadLine, [231_TimePlan].FinishedDate,"
                + " [231_TimePlan].UnitTime, [231_TimePlan].Remark"
                + " FROM [231_TimePlan] LEFT OUTER JOIN [222_Customer] ON [222_Customer].CustomerID = [231_TimePlan].CustomerID"
                + " ORDER BY [231_TimePlan].TimePlanNo";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridF4Plan.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("TimePlanNo", typeof(string));
                newTable.Columns.Add("PlanNo", typeof(string));
                newTable.Columns.Add("CompID", typeof(string));
                newTable.Columns.Add("NewComp", typeof(string));
                newTable.Columns.Add("Quantity", typeof(int));
                newTable.Columns.Add("SubOrder1", typeof(string));
                newTable.Columns.Add("SubOrder2", typeof(string));
                newTable.Columns.Add("SubOrder3", typeof(string));
                newTable.Columns.Add("SubOrder4", typeof(int));
                newTable.Columns.Add("SubOrder5", typeof(string));
                newTable.Columns.Add("SubOrder6", typeof(string));
                newTable.Columns.Add("Customer", typeof(string));
                newTable.Columns.Add("FromSerialNo", typeof(string));
                newTable.Columns.Add("ToSerialNo", typeof(string));
                newTable.Columns.Add("DeadLine", typeof(DateTime));
                newTable.Columns.Add("FinishedDate", typeof(DateTime));
                newTable.Columns.Add("UnitTime", typeof(int));
                newTable.Columns.Add("Remark", typeof(string));
                gridF4Plan.DataSource = newTable;
            }
            F4Plan.Columns[0].Caption = "Time Plan No";
            F4Plan.Columns[1].Caption = "Plan No.";
            F4Plan.Columns[2].Caption = "Comp. ID.";
            F4Plan.Columns[3].Caption = "New Comp.";
            F4Plan.Columns[4].Caption = "Quantity";
            F4Plan.Columns[5].Caption = "Sub. Order 1";
            F4Plan.Columns[6].Caption = "Sub. Order 2";
            F4Plan.Columns[7].Caption = "Sub. Order 3";
            F4Plan.Columns[8].Caption = "Sub. Order 4";
            F4Plan.Columns[9].Caption = "Sub. Order 5";
            F4Plan.Columns[10].Caption = "Sub. Order 6";
            F4Plan.Columns[11].Caption = "Customer";
            F4Plan.Columns[12].Caption = "From Serial No.";
            F4Plan.Columns[13].Caption = "To Searial No.";
            F4Plan.Columns[14].Caption = "Deadline";
            F4Plan.Columns[15].Caption = "Finished Date";
            F4Plan.Columns[16].Caption = "Unit Time";
            F4Plan.Columns[17].Caption = "Remark";
            //F4Plan.Columns[0].Visible = false;
            F4Plan.BestFitColumns();
            ModuleWaitDialog.CloseWaitDialog();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }


        private void buttonEdit_Click(object sender, EventArgs e)
        {
            
            callEditForm(); 
        }

        private void buttonFinishedReport_Click(object sender, EventArgs e)
        {
            frm_CheckFinished_F4Plan checkFinishedForm = new frm_CheckFinished_F4Plan();
            checkFinishedForm.ShowDialog();
        }
    }
}
